package com.da.lon.wang.xlg.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.da.lon.wang.xlg.R;
import com.da.lon.wang.xlg.bean.CouponSnBean;
import com.da.lon.wang.xlg.constants.PreferencesConfig;
import com.da.lon.wang.xlg.constants.XLGConstants;
import com.da.lon.wang.xlg.http.HttpLoader;
import com.da.lon.wang.xlg.http.HttpLoader.OnWebLoadListener;
import com.da.lon.wang.xlg.util.PreferenceUtils;
import com.da.lon.wang.xlg.util.XLGDataUtils;
import com.umeng.analytics.MobclickAgent;

import net.tsz.afinal.http.AjaxParams;

public class Coupon2Fragment extends BaseFragment implements OnClickListener {

    private Context context = null;
    private String user_id = null;
    private String deviceId = null;
    private String user_key = null;
    private String ids = null;
    private String data_store = null;
    private int resultCode = 2;

    private EditText couponEditText;
    private Button couponButton;
    private View view;

    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("Coupon2Fragment");
        MobclickAgent.onResume(getActivity());
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("Coupon2Fragment");
        MobclickAgent.onPause(getActivity());
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected View inflateView(LayoutInflater inflater, ViewGroup var2) {
        // TODO Auto-generated method stub
        view = inflater.inflate(R.layout.ouka_coupon2_fragment, null);
        return view;
    }

    @Override
    protected void initView() {
        // TODO Auto-generated method stub
        context = getActivity();

        user_id = PreferenceUtils.getInstance().getSettingStr(
                PreferencesConfig.USER_ID, "");
        deviceId = PreferenceUtils.getInstance().getSettingStr(
                PreferencesConfig.DEVICE_ID, "");
        user_key = PreferenceUtils.getInstance().getSettingStr(
                PreferencesConfig.USER_KEY, "");
        ids = getArguments().getString("ids");
        data_store = getArguments().getString("data_store");

        couponEditText = (EditText) view.findViewById(R.id.ouka_coupon_et);
        couponButton = (Button) view.findViewById(R.id.ouka_coupon_sure);
        couponButton.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ouka_coupon_sure:
                try {
                    String url = XLGConstants.URL + "Cart-couponsn";
                    String coupon_sn = couponEditText.getText().toString();

                    if (coupon_sn != null && coupon_sn.trim().length() > 0) {
                        HttpLoader<CouponSnBean> loader = new HttpLoader<CouponSnBean>(
                                context, url, CouponSnBean.class);
                        AjaxParams params = new AjaxParams();
                        params.put(
                                "token",
                                XLGDataUtils.encrypt2MD5("Cart"
                                        + XLGConstants.SECRET + "couponsn"));
                        params.put("deviceId", deviceId);
                        params.put("version", "21");
                        params.put("ids", ids);
                        if (data_store != null && !"".equals(data_store)) {
                            params.put("data_store", data_store);
                        }
                        params.put("id", user_id);
                        params.put("user_key", user_key);
                        params.put("coupon_sn", coupon_sn);
                        loader.loadData(params);

                        loader.setManagerListener(new OnWebLoadListener<CouponSnBean>() {

                            @Override
                            public void OnStart() {
                                showDialog();
                            }

                            @Override
                            public void OnError(String error) {
                                dismissDialog();
                            }

                            @Override
                            public void onSuccess(CouponSnBean t) {
                                dismissDialog();
                                if (t != null && "000000".equals(t.getCode())
                                        && t.getResult() != null) {
                                    Toast.makeText(context, t.getMessage(),
                                            Toast.LENGTH_SHORT).show();

                                    String couponId = t.getResult().getCoupon();

                                    Intent intent = new Intent();

                                    intent.putExtra("couponId", couponId);
                                    getActivity().setResult(resultCode, intent);
                                    getActivity().finish();
                                } else {
                                    Toast.makeText(context, t.getMessage(),
                                            Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                    } else {
                        Toast.makeText(context, "请输入代金券号！", Toast.LENGTH_SHORT)
                                .show();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;

            default:
                break;
        }
    }

    @Override
    protected void RefreshData() {
        // TODO Auto-generated method stub

    }

}
